package org.jeecg.modules.mr.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.mr.entity.MrBookMeters;
import org.jeecg.modules.mr.mapper.MrBookMetersMapper;
import org.jeecg.modules.mr.service.IMrBookMetersService;
import org.jeecg.modules.online.cgform.service.IOnlCgformFieldService;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 抄表顺序
 * @Author: jeecg-boot
 * @Date:   2022-02-03
 * @Version: V1.0
 */
@Service
public class MrBookMetersServiceImpl extends ServiceImpl<MrBookMetersMapper, MrBookMeters> implements IMrBookMetersService {

    @Resource
    private MrBookMetersMapper bookMetersMapper;

    @Autowired
    private IOnlCgformHeadService formHeadService;
    @Autowired
    private IOnlCgformFieldService formFieldService;
    @Override
    public JSONArray queryBookTree(Map<String, Object> requestParams) {
        List<Map<String, Object>> mapList = queryBookList(requestParams);

        JSONArray rsAreaArray = new JSONArray();

        JSONArray areaArray = new JSONArray();

        if(null != mapList && mapList.size()>0){
            Set<String>areaSet=new HashSet<>();
            for (Map<String, Object> itemMap : mapList) {

                JSONObject itemObject = new JSONObject() ;
                String areaId =itemMap.get("area_id")+""; //父级ID


                if("0".equals(areaId)){
                    String areaName = itemMap.get("book_name")+"";
                    String id =itemMap.get("id")+""; //父级ID
                    itemObject.put("pid",id);
                    itemObject.put("title",areaName);
                    areaArray.add(itemObject);
                }
//                JSONArray jsonArray = new JSONArray();
//                for (Map<String, Object> itemMap2 : mapList) {
//                    String areaId2 = itemMap2.get("area_id")+"";
//                    if(areaId.equals(areaId2)){
//                        jsonArray.add(JSON.parseObject(JSONObject.toJSONString(itemMap2)));
//                    }
//                }
//                areaObject.put("areaId",areaKey);
//                areaObject.put("areaData",jsonArray);
//                areaArray.add(areaObject);
            }
        }



        if(null != areaArray && areaArray.size()>0) {
            for (Object obj : areaArray) {
                JSONObject areaObject = (JSONObject) obj;
                String areaId = areaObject.getString("pid");
                String title = areaObject.getString("title");

                JSONObject itemObject = new JSONObject() ;
                itemObject.put("key",areaId);
                itemObject.put("title",title);

                JSONArray subAreaArray = new JSONArray();
                for (Map<String, Object> itemMap : mapList) {
                    String pid =itemMap.get("area_id")+""; //父级ID
                    if(pid.equals(areaId)){
                        String bookName = itemMap.get("book_name")+"";
                        String id =itemMap.get("id")+""; //ID
                        JSONObject subItemObject = new JSONObject() ;
                        subItemObject.put("title",bookName);
                        subItemObject.put("key",id);
                        subAreaArray.add(subItemObject);
                    }
                }
                if(subAreaArray.size()>0){
                    itemObject.put("children",subAreaArray);
                }
                rsAreaArray.add(itemObject);
            }
        }
        return rsAreaArray;
    }

    @Override
    public List<Map<String, Object>> queryBookList(Map<String, Object> requestParams) {
        StringBuffer sb= new StringBuffer();
//        sb.append("select");
//        sb.append(" t2.name as area_name,");
//        sb.append(" t1.name as book_name,");
//        sb.append(" t2.id as area_id,");

        sb.append("select");
        sb.append(" t1.pname as area_name,");
        sb.append(" t1.name as book_name,");
        sb.append(" t1.pid as area_id,");

        sb.append(" t1.id,");
        sb.append(" t1.reader_id,");
        sb.append(" t1.water_read_cycle,");
        sb.append(" t1.water_read_times,");
        sb.append(" t1.del_flag");
        sb.append(" from mws_t_mr_books t1");
//        sb.append(" Left JOIN mws_t_base_areas t2 on t1.AREA_ID=t2.id");

        StringBuffer con=new StringBuffer();
        con.append(" where 1=1");
        if(null !=  requestParams){
            requestParams.forEach((key,value) ->{
                if( value instanceof Integer || value instanceof Long
                        || value instanceof Double || value instanceof BigDecimal){
                    con.append(" and t1."+key+" = "+value);
                } else{
                    con.append(" and t1."+key+" = '"+value+"'");
                }
            });
            if(con.length()>5){
                sb.append(con);
            }
        }
        sb.append(" order by t1.pid,t1.sort");
        List<Map<String, Object>> mapList = formFieldService.queryListBySql(sb.toString());
        return mapList;
    }

    @Override
    public List<Map<String, Object>> queryBookReadMater(String bookId,String keyword) {
        return bookMetersMapper.queryBookReadMater(bookId,keyword);
    }

    @Override
    public List<Map<String, Object>> queryBookOrderMater(String bookId) {
        return bookMetersMapper.queryBookOrderMater(bookId);
    }

    @Override
    public void saveOrderMater(JSONArray orderData) {
        for (Object orderDatum : orderData) {
            JSONObject item=(JSONObject)orderDatum;
            bookMetersMapper.updateOrderMeter(item.getString("bookMeterId"),item.getInteger("orderNo"));
        }
    }
}
